package com.example.androidtest.listview;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.androidtest.R;

public class MainActivity extends AppCompatActivity {
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview);
        listView = findViewById(R.id.listview);
        final MyAdapter adapter = new MyAdapter();
        listView.setAdapter(adapter);
        AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String s = adapter.names[position];
                Intent intent = new Intent(MainActivity.this, IntentActivity.class);
                intent.putExtra("intent", s);
                startActivity(intent);
            }
        };
        listView.setOnItemClickListener(listener);
    }

    class MyAdapter extends BaseAdapter {
        int[] img = {R.drawable.grape, R.drawable.apple, R.drawable.banana, R.drawable.cherry, R.drawable.pear, R.drawable.pineapple, R.drawable.watermelon, R.drawable.strawberry};
        String[] names = {"grape", "apple", "banana", "cherry", "pear", "pineapple", "watermelon", "strawberry"};

        @Override

        public int getCount() {
            return names.length;
        }

        @Override
        public Object getItem(int position) {
            return names[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
     /*       View view = View.inflate(Ch2_marketActivity.this, R.layout.activity_ch2_market_item, null);
            ImageView imageView = view.findViewById(R.id.ch2_market_item_image);
            TextView textView = view.findViewById(R.id.ch2_market_tv_item);
            imageView.setImageResource(img[position]);
            textView.setText(names[position]);
            return view;*/


            /*数据量过大时，为了优化ListView减少耗时操作，利用ViewHolder类*/
            ViewHolder viewHolder = new ViewHolder();
            if (convertView == null) {
                convertView = View.inflate(MainActivity.this, R.layout.activity_item, null);
                viewHolder.imageView = convertView.findViewById(R.id.item_image);
                viewHolder.textView = convertView.findViewById(R.id.item_tv_text);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.imageView.setImageResource(img[position]);
            viewHolder.textView.setText(names[position]);
            return convertView;
        }
    }

    class ViewHolder {
        ImageView imageView;
        TextView textView;

    }
}